//引入重置样式
require("../css/commCss/normalize.css");
//引入重置扩展样式
require("../css/commCss/reset.css");
//引入公共样式
require("../css/commCss/property.css");
//引入公共扩展样式
require("../css/commCss/basic.css");

//引入字体图标样式
require("../lib/font_3139541_zokbtoztf/iconfont.css");

//引入当前页面样式
require("../css/main.less");

//导入axios
const axios = require("axios");

//导入weui
require("weui");
const weui = require("weui.js");

//导入util
const util = require("../lib/util");

//窗口加载事件
document.addEventListener("DOMContentLoaded", function () {
  /*
   *0获取元素
   */
  let imgurl = document.querySelector(".imgurl"); //头像
  let imgUrl = document.querySelector("#imgUrl"); //头像
  let nickname = document.querySelector(".nickname"); //昵称
  let sign = document.querySelector(".sign"); //个性签名
  let loginOut = document.querySelector("#loginOut"); //退出登录
  let dynamicCount = document.querySelector(".dynamicCount"); //个性动态
  let movementTime = document.querySelector("#movementTime"); //总运动分钟数
  let movementKilocalorie = document.querySelector("#movementKilocalorie"); //周消耗多少千卡
  let mainUsername = document.querySelector("#mainUsername"); //获取头部

  //  console.log(imgurl,nickname,sign,loginOut,movementTime,movementKilocalorie,mainUsername);

  /**
   * 1.底部导航
   */
  util.fixBottomNav("main");

  /**
   * 2.发送请求
   */
  axios
    .get("http://139.9.177.51:8099/users/mysportsBadge", {
      params: {
        //获取用户id
        userId: localStorage.getItem("userId"),
      },
    })
    .then(function (res) {
      console.log(res);
      let user = res.data.data.user;
      //如果是新账号,除了有账号用户名:默认手机号,其他信息都为null,所以在渲染的时候需要判断
      //渲染头像
      if (user.imgurl) {
        imgUrl.src = user.imgurl;
      }
      // console.log(imgurl.src);
      //渲染用户名
      nickname.textContent = user.nickname;
      //渲染个性签名
      if (user.sign) {
        sign.textContent = user.sign;
      }
      //渲染个人动态
      dynamicCount.textContent = res.data.data.dynamicCount;

      //渲染总运动分钟数
      movementTime.textContent = res.data.data.sports.times;

      //渲染周消耗多少千卡
      movementKilocalorie.textContent = res.data.data.sports.calorie;
    });

  //点击退出登录注册事件
  loginOut.addEventListener("click", function () {
    weui.confirm("您是否确定要退出?不可逆哦!", function () {
      // console.log("您点击了确定按钮"); //有两个选择取消,和确定,点击确定就会弹出已确定退出
      //清空本地存储账号数据
      localStorage.clear();

      //跳转到登录页面
      location.href = "login.html";
    });
  });

  //点击头部进行跳转
  mainUsername.addEventListener("click", function () {
    // console.log(1);
    //跳转到个人资料页面
    location.href="userinfo.html"
  });
});
